package com.linkage.event.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.base.ResponseInfo;
import com.linkage.event.pojo.entity.EventAttention;
import com.linkage.event.pojo.entity.EventDiscusses;
import com.linkage.event.pojo.vo.ReplyVo;
import com.linkage.event.service.ProblemService;
import com.linkage.event.utils.UserInterfUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.bind.ValidationException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/problem")
@Slf4j
public class ProblemController {

    @Resource
    private ProblemService problemService;

    /**
     * 事件评论列表
     * @param requestInfo
     * @return
     */
    @RequestMapping(value = "/queryProblemList", method = RequestMethod.POST)
    public String listEventDiscussesShow(@RequestBody RequestInfo<EventDiscusses> requestInfo){
        try {
            String token = requestInfo.getBase().getUsertoken();
            if(StrUtil.isEmpty(token)){
                token = "TOKEN:POLICE-0001:CJGLY-3:13403404556";
            }
            Integer pageNo = Integer.parseInt(requestInfo.getBase().getPageNum());
            Integer pageSize = Integer.parseInt(requestInfo.getBase().getPageRows());
            PageInfo<EventDiscusses> pageInfo =  this.problemService.queryProblemListByPage(requestInfo.getBuss(), pageNo, pageSize,token);
            return ResponseInfo.successWithPage(pageInfo).toString();
        } catch (Exception e) {
            log.error("",e);
            return ResponseInfo.error(e.getMessage()).toString();
        }
    }

    /**
     * 查询评论详情
     * @param requestInfo
     * @return
     */
    @PostMapping("/queryProblemInfo")
    public String queryProblemInfo(@RequestBody RequestInfo<Map> requestInfo){
        if(!requestInfo.getBuss().containsKey("problemId")){
            return ResponseInfo.error("问题主键为空！").toString();
        }
        EventDiscusses vo = problemService.queryProblemInfo(String.valueOf(requestInfo.getBuss().get("problemId")));
        return ResponseInfo.success(vo).toString();
    }

    /**
     * 创建问题
     * @param eds
     * @return
     */
    @RequestMapping(value = "/createDiscusses", method = RequestMethod.POST)
    public String createDiscusses(@RequestBody RequestInfo<EventDiscusses> eds){
        try {
            if(StrUtil.isEmpty(eds.getBuss().getEventTypeId())){
                return ResponseInfo.error("所属模块为空！").toString();
            }
            this.problemService.createDiscusses(eds.getBuss(),eds.getBase().getUsertoken());
            return ResponseInfo.success("创建成功").toString();
        } catch (Exception e) {
            log.error("",e);
            return ResponseInfo.error(e.getMessage()).toString();
        }
    }

    /**
     * 创建官方回复
     * @param requestInfo
     * @return
     */
    @RequestMapping(value = "/createReplies", method = RequestMethod.POST)
    public String  createReplies(@RequestBody RequestInfo<ReplyVo> requestInfo){
        ReplyVo replyVo = requestInfo.getBuss();
        if(StrUtil.isEmpty(replyVo.getProblemId())){
            return ResponseInfo.error("所属模块为空！").toString();
        }
        if(StrUtil.isEmpty(replyVo.getReplyContent())){
            return ResponseInfo.error("回复内容为空！").toString();
        }
        try {
            replyVo.setIsOfficial("1");
            this.problemService.createReply(replyVo,requestInfo.getBase().getUsertoken());
            return ResponseInfo.success("回复成功").toString();
        } catch (Exception e) {
            log.error("",e);
            return ResponseInfo.error(e.getMessage()).toString();
        }
    }

    /**
     * 创建其他用户回复
     * @param requestInfo
     * @return
     */
    @RequestMapping(value = "/addUserReplies", method = RequestMethod.POST)
    public String  addUserReplies(@RequestBody RequestInfo<ReplyVo> requestInfo){
        ReplyVo replyVo = requestInfo.getBuss();
        if(StrUtil.hasEmpty(replyVo.getProblemId(),replyVo.getUserId())){
            return ResponseInfo.error("问题id或者用户id为空！").toString();
        }
        if(StrUtil.isEmpty(replyVo.getReplyContent())){
            return ResponseInfo.error("回复内容为空！").toString();
        }
        try {
            replyVo.setIsOfficial("0");
            this.problemService.createReply(requestInfo.getBuss(),requestInfo.getBase().getUsertoken());
            return ResponseInfo.success("回复成功").toString();
        } catch (Exception e) {
            log.error("",e);
            return ResponseInfo.error(e.getMessage()).toString();
        }
    }

    /**
     * 删除培训信息
     * @param requestInfo
     * @return
     */
    @PostMapping("/deleteProblem")
    public String deleteProblem(@RequestBody RequestInfo<Map> requestInfo){
        if(!requestInfo.getBuss().containsKey("problemId")){
            return ResponseInfo.error("问题主键为空！").toString();
        }
        problemService.deleteProblem(String.valueOf(requestInfo.getBuss().get("problemId")));
        return ResponseInfo.success("删除成功").toString();
    }

    /**
     * 查询我的关注
     * @param requestInfo
     * @return
     */
    @PostMapping("/myAttention")
    public String myAttention(@RequestBody RequestInfo<EventAttention> requestInfo){
        if(StrUtil.isEmpty(requestInfo.getBuss().getUserId())){
            return ResponseInfo.error("userId参数为空").toString();
        }
        List<EventDiscusses> list = problemService.myAttention(requestInfo.getBuss().getUserId());
        return ResponseInfo.success(list).toString();
    }
    /**
     * 一键关注
     * @param ea
     * @return
     */
    @RequestMapping(value = "/createAttention", method = RequestMethod.POST)
    public String createAttention(@RequestBody RequestInfo<EventAttention> ea){
        try{
            this.problemService.createAttention(ea.getBuss());
            return JSONUtil.toJsonStr(ResponseInfo.success());
        }catch(Exception e){
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }

    /**
     * 取消关注
     * @param eak
     * @return
     */
    @RequestMapping(value = "/cancelAttention", method = RequestMethod.POST)
    public String cancelAttention(@RequestBody RequestInfo<EventAttention> eak){
        if(StrUtil.hasEmpty(eak.getBuss().getProblemId(),eak.getBuss().getUserId())){
            return ResponseInfo.error("问题id或者用户id为空！").toString();
        }
        try{
            this.problemService.cancelAttention(eak.getBuss());
            return JSONUtil.toJsonStr(ResponseInfo.success());
        }catch(Exception e){
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }
}
